package com.huluobo.a2209monthdemo.view

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.huluobo.a2209monthdemo.R
import com.huluobo.lib_base.RoutePath

/**
 *  Created by LC on 2025/3/13.
 */

data class HomeEntity(val img: Int, val title: String)

@Composable
fun HomePage(navHostController: NavHostController) {
    val list = listOf(
        HomeEntity(R.drawable.apps, "在线应用"),
        HomeEntity(R.drawable.sign, "考勤打卡"),
        HomeEntity(R.drawable.document, "文件"),
        HomeEntity(R.drawable.report, "工作日志"),
        HomeEntity(R.drawable.shenpi, "智能审批"),
        HomeEntity(R.drawable.question, "任务")
    )

    val list2 = listOf(
        HomeEntity(R.drawable.contact, "联系人"),
        HomeEntity(R.drawable.group, "讨论组"),
        HomeEntity(R.drawable.department, "组织架构")
    )

    Column {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .background(colorResource(id = R.color.lightblue))
                .padding(10.dp)
        ) {
            Text(
                text = "12", color = Color.White, modifier = Modifier
                    .background(
                        colorResource(id = R.color.pink),
                        CircleShape
                    )
                    .padding(5.dp)
            )
            Spacer(modifier = Modifier.width(10.dp))
            Text(text = "主门户", fontSize = 18.sp)
        }

        TitleItem(img = R.drawable.icon1382, title = "应用")
        LazyVerticalGrid(verticalArrangement = Arrangement.spacedBy(10.dp),
            columns = GridCells.Fixed(3), content = {
                items(list) {
                    ContentItem(entity = it) {
                        when (it.title) {
                            "在线应用" -> navHostController.navigate(RoutePath.APPS)
                            "文件" -> navHostController.navigate(RoutePath.DOCUMENT)
                            "工作日志" -> navHostController.navigate(RoutePath.REPORT)
                            "考勤打卡" -> navHostController.navigate(RoutePath.SIGN)
                        }
                    }
                }
            })
        TitleItem(img = R.drawable.icon, title = "通信")
        LazyVerticalGrid(verticalArrangement = Arrangement.spacedBy(10.dp),
            columns = GridCells.Fixed(3), content = {
                items(list2) {
                    ContentItem(entity = it) {

                    }
                }
            })
        TitleItem(img = R.drawable.icon, title = "公众号")
    }
}

@Composable
fun TitleItem(img: Int, title: String) {
    Row(Modifier.padding(10.dp), verticalAlignment = Alignment.CenterVertically) {
        Image(painter = painterResource(id = img), contentDescription = "")
        Spacer(modifier = Modifier.width(10.dp))
        Text(text = title)
    }
}

@Composable
fun ContentItem(entity: HomeEntity, onClick: () -> Unit) {
    Column(
        Modifier
            .padding(10.dp)
            .clickable {
                onClick()
            }, horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Image(painter = painterResource(id = entity.img), contentDescription = "", Modifier.size(40.dp))
        Spacer(modifier = Modifier.height(10.dp))
        Text(text = entity.title)
    }
}